/**
 * Created by Jacky.Gao on 2017-10-23.
 */
import Property from './Property.js';
export default class DatetimeProperty extends Property {
    constructor() {
        super();
        this.init();
    }
    init() {
        this.positionLabelGroup = this.buildPositionLabelGroup();
        this.col.append(this.positionLabelGroup);
        this.col.append(this.buildBindParameter());
        this.col.append(this.buildLabelGroup());
        var formatGroup = $("<div class='form-group'><label class='control-label'>日期格式</label></div>");
        this.col.append(formatGroup);
        this.formatSelect = $("<select class='form-control'>");
        this.formatSelect.append($("<option>yyyy-mm-dd</option>"));
        this.formatSelect.append($("<option>yyyy-mm-dd hh:ii:ss</option>"));
        var self = this;
        this.formatSelect.change(function () {
            self.current.setDateFormat($(this).val());
            self.setFormat($(this).val())
            $('#datePickerinputGroup').val('')
        });
        formatGroup.append(this.formatSelect);

    }
    setFormat(val) {
        this.dateFormat = val;
        this.datePickerinputGroup.datetimepicker('remove');
        this.options = {
            format: this.dateFormat,
            autoclose: 1
        };
        if (this.dateFormat === 'yyyy-mm-dd') {
            this.options.startView = 2;
            this.options.minView = 2;
        }
        this.datePickerinputGroup.datetimepicker(this.options);
    }
    refreshValue(current) {
        super.refreshValue(current);
        this.formatSelect.val(current.dateFormat);
        if ($('#dateTimeID')) $('#dateTimeID').remove()
        this.defaultValueGroup = $("<div class='form-group' id='dateTimeID'><label class='control-label'>默认值</label></div>");
        this.col.append(this.defaultValueGroup);
        this.defaultSelect = $("<select class='form-control check'>");
        this.defaultSelect.append($("<option value='1'>自定义</option>"));
        this.defaultSelect.append($("<option value='2'>系统</option>"));
        this.defaultValueGroup.append(this.defaultSelect);
        this.defaultSelect.val(current.defaultType || '2')
        var self = this
        let defaultValue = current.showValue ? current.showValue : (current.defaultValue || '')
        this.currentTime = $(`<select class="form-control">
         <option value="当前时间">当前时间</option>
         </select>`);
        this.inputElement = $("<div>");
        this.datePickerinputGroup = $("<div class='input-group date'>");
        this.inputElement.append(this.datePickerinputGroup);
        var text = $(`<input type='text' value='${defaultValue}' id='datePickerinputGroup' class='form-control'>`)
        this.datePickerinputGroup.append(text);
        var pickerIcon = $("<span class='input-group-addon'><span class='glyphicon glyphicon-calendar'></span></span>");
        this.datePickerinputGroup.append(pickerIcon);
        this.datePickerinputGroup.datetimepicker({
            format: current.dateFormat,
            autoclose: 1,
            startView: 2,
            minView: 2
        });
        const _this = this;
        this.defaultValueGroup.append(this.currentTime)
        this.defaultValueGroup.append(this.inputElement)
        if (this.defaultSelect.val() === '1') {
            _this.currentTime.hide()
            _this.inputElement.show()
        } else {
            _this.currentTime.show()
            _this.inputElement.hide()
        }
        this.defaultSelect.change(function () {
            if ($(this).val() === "2") {
                _this.currentTime.show()
                _this.inputElement.hide()
            }
            if ($(this).val() === "1") {
                $('#datePickerinputGroup').val('')
                _this.currentTime.hide()
                _this.inputElement.show()
            }
            self.current.setDefaultType($(this).val())
        });
        self.datePickerinputGroup.change(function () {
            defaultValue = text.val()
            self.current.setDefaultValue(defaultValue);
        })
        function timestampToTime() {
            let value = ''
            var date = new Date();
            var Y = date.getFullYear() + "-";
            var M = (date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1) + "-";
            var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
            var h = date.getHours() + ":";
            var m = date.getMinutes() + ":";
            var s = date.getSeconds();
            value = self.current.dateFormat === 'yyyy-mm-dd' ? Y + M + D : Y + M + D + h + m + s
        }
    }
}